package com.lams.service.system.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lams.jdbc.IBaseDAO;
import com.lams.jdbc.QueryUtils;
import com.lams.service.BaseFacade;
import com.lams.service.system.IParameterFacade;
import com.lams.util.ResultEntity;

public class ParameterFacade extends BaseFacade implements IParameterFacade {
	private String saveCode;
	private String deleteCode;
	private String updateCode;
	private String findByCode;
	private String findByOrg;
	@Override
	public void saveOrUpdate(Map param) {
		
		if(param.containsKey("PARAMETER_ID") && param.get("PARAMETER_ID")!=null)
			this.update(param);
		else
			this.adu(saveCode, param);
	}

	@Override
	public void delete(Map param) {
		this.adu(deleteCode, param);
	}

	@Override
	public void update(Map param) {
		this.adu(updateCode, param);
		// TODO Auto-generated method stub
//		pd.update(param);
	}

	@Override
	public ResultEntity findByUnit(int start, int limit, String orgNo) {
		Map params = new HashMap();
		params.put("ORG_NO", orgNo);
		String sql = QueryUtils.getSqlBySqlCode(findByOrg, params);
		List<Map> list = QueryUtils.executeQuery(start, limit, sql);
		ResultEntity page = new ResultEntity(list,QueryUtils.getTotalCount(sql));
		return page;
	}
	private void adu(String code,Map param){
		//Map params = SParameter.decode(param);
		String sql = QueryUtils.getSqlBySqlCode(code, param);
		try {
			this.getDao().executeUpdate(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public ResultEntity findParameterByName(String parameName) {
		Map params = new HashMap();
		params.put("property", "PARAMETER_NAME");
		params.put("value", parameName);
		String sql = QueryUtils.getSqlBySqlCode(findByCode, params);
		ResultEntity result = new ResultEntity(QueryUtils.executeQuery(sql),QueryUtils.getTotalCount(sql));
		return result;
	}
	@Override
	public ResultEntity checkParameterName(String parameterName) {
		Map params = new HashMap();
		params.put("property", "PARAMETER_NAME");
		params.put("value", parameterName);
		String sql = QueryUtils.getSqlBySqlCode(findByCode, params);
		ResultEntity result = new ResultEntity(null,QueryUtils.getTotalCount(sql));
		result.setSuccess(result.getTotalCount()==0?true:false);
		return result;
	}
	public String getSaveCode() {
		return saveCode;
	}

	public void setSaveCode(String saveCode) {
		this.saveCode = saveCode;
	}

	public String getDeleteCode() {
		return deleteCode;
	}

	public void setDeleteCode(String deleteCode) {
		this.deleteCode = deleteCode;
	}

	public String getUpdateCode() {
		return updateCode;
	}

	public void setUpdateCode(String updateCode) {
		this.updateCode = updateCode;
	}

	public String getFindByCode() {
		return findByCode;
	}

	public void setFindByCode(String findByCode) {
		this.findByCode = findByCode;
	}

	public String getFindByOrg() {
		return findByOrg;
	}

	public void setFindByOrg(String findByOrg) {
		this.findByOrg = findByOrg;
	}
}
